Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@smithy/middleware-stack

Package Overview
Dependencies
Maintainers
2
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@smithy/middleware-stack

Provides a means for composing multiple middleware functions into a single handler

  • 3.0.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
18M
increased by3.22%
Maintainers
2
Weekly downloads
 
Created

What is @smithy/middleware-stack?

@smithy/middleware-stack is a middleware stack implementation for the AWS SDK for JavaScript. It allows developers to compose middleware functions to handle requests and responses, enabling customization and extension of the SDK's behavior.

What are @smithy/middleware-stack's main functionalities?

Adding Middleware

This feature allows you to add middleware to the stack. Middleware functions can intercept and modify requests and responses. The code sample demonstrates adding a logging middleware that logs the request and response.

const { MiddlewareStack } = require('@smithy/middleware-stack');

const stack = new MiddlewareStack();

const loggingMiddleware = (next) => async (args) => {
  console.log('Request:', args);
  const result = await next(args);
  console.log('Response:', result);
  return result;
};

stack.add(loggingMiddleware, {
  step: 'initialize',
  name: 'loggingMiddleware',
});

// Example usage with a handler
const handler = async (args) => {
  return { data: 'response data' };
};

const composedHandler = stack.resolve(handler, {});
composedHandler({ input: 'request data' });

Removing Middleware

This feature allows you to remove middleware from the stack by its name. The code sample demonstrates adding and then removing a logging middleware.

const { MiddlewareStack } = require('@smithy/middleware-stack');

const stack = new MiddlewareStack();

const loggingMiddleware = (next) => async (args) => {
  console.log('Request:', args);
  const result = await next(args);
  console.log('Response:', result);
  return result;
};

stack.add(loggingMiddleware, {
  step: 'initialize',
  name: 'loggingMiddleware',
});

// Remove the middleware
stack.remove('loggingMiddleware');

Composing Middleware

This feature allows you to compose multiple middleware functions in a stack. The code sample demonstrates adding two middleware functions and composing them to handle a request.

const { MiddlewareStack } = require('@smithy/middleware-stack');

const stack = new MiddlewareStack();

const middleware1 = (next) => async (args) => {
  console.log('Middleware 1');
  return next(args);
};

const middleware2 = (next) => async (args) => {
  console.log('Middleware 2');
  return next(args);
};

stack.add(middleware1, { step: 'initialize', name: 'middleware1' });
stack.add(middleware2, { step: 'initialize', name: 'middleware2' });

// Example usage with a handler
const handler = async (args) => {
  return { data: 'response data' };
};

const composedHandler = stack.resolve(handler, {});
composedHandler({ input: 'request data' });

Other packages similar to @smithy/middleware-stack

FAQs

Package last updated on 21 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc